package com.rzcf.blink.pay.trigger.job;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * XXL-Job
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class XXLJob {


    @XxlJob("demoJobHandler")
    public void doJob() {
        // 获取任务参数
        String param = XxlJobHelper.getJobParam();
        long jobId = XxlJobHelper.getJobId();
        log.info("执行任务 - XXL-Job - 任务ID: {}, 参数: {}", jobId, param);
        XxlJobHelper.log("开始执行任务，任务ID: {}, 参数: {}", jobId, param);

        try {
            // 处理业务逻辑
            String message = "JOB服务进入MSG";
            if (param != null && !param.isEmpty()) {
                message += " - 参数: " + param;
            }

            Boolean send = true;
            if (!send) {
                XxlJobHelper.log("MQ消息发送失败: {}", message);
                // 标记任务执行失败
                XxlJobHelper.handleFail("MQ消息发送失败: " + message);
                return;
            }
            XxlJobHelper.log("MQ消息发送成功: {}", message);

        } catch (Exception e) {
            log.error("任务执行失败", e);
            XxlJobHelper.log("任务执行失败: {}", e.getMessage());
            // 标记任务执行失败
            XxlJobHelper.handleFail("任务执行失败: " + e.getMessage());
            return;
        }

        // 标记任务执行成功
        XxlJobHelper.handleSuccess("任务执行成功");
    }

}
